package com.handwriting.backend.controller;

import com.handwriting.backend.entity.Dataset;
import com.handwriting.backend.message.CreateDataset;
import com.handwriting.backend.message.DataFileInfo;
import com.handwriting.backend.message.Response;
import com.handwriting.backend.service.DatasetService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
public class DatasetController {
    private final DatasetService service;

    public DatasetController(DatasetService service) {
        this.service = service;
    }

    @Operation(summary = "获取所有数据集详细信息")
    @GetMapping(value = "/dataset/all")
    public Response<List<Dataset>> getAllDatasets() {
        try {
            List<Dataset> datasets = service.getAllDatasets();
            return Response.ok(datasets);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error finding dataset information.");
        }
    }

    @Operation(summary = "删除指定数据集")
    @DeleteMapping(value = "/dataset/{id}")
    public Response<Object> deleteDataset(@PathVariable int id) {
        try {
            service.deleteDataset(id);
            return Response.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error deleting dataset " + id + ".");
        }
    }

    @Operation(summary = "创建数据集")
    @PostMapping(value = "/dataset")
    public Response<Object> createDataset(@RequestBody CreateDataset cd) {
        try {
            service.createDataset(cd.getName(), cd.getImageIndexes());
            return Response.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error creating dataset with images " + cd.getImageIndexes() + ".");
        }
    }

    @Operation(summary = "上传zip数据")
    @PostMapping(value = "/dataset/files", consumes = "multipart/form-data")
    public Response<Object> uploadDatasets(@RequestPart("file") MultipartFile file) {
        try {
            service.uploadDatasets(file);
            return Response.ok(null);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error uploading file " + file.getName() + ".");
        }
    }

    @Operation(summary = "获取所有图片信息")
    @GetMapping(value = "/dataset/images")
    public Response<List<DataFileInfo>> getImagesInfo() {
        try {
            List<DataFileInfo> files = service.getDataFiles();
            return Response.ok(files);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.error("Error getting image datasets");
        }
    }

    @Operation(summary = "获取图片")
    @GetMapping(value = "/dataset/file/**")
    public Response<Object> getImage(HttpServletRequest request, HttpServletResponse response) {
        try {
            service.getFile(request, response);
            return Response.ok(null);
        } catch (IOException e) {
            e.printStackTrace();
            return Response.error("No such file");
        }
    }
}
